/*
 * Copyright (c) 2023.11.29 Fengxun
 * Version:5.2.2
 */

// type=100 图文显示
const {
  getCheckpointList,
  saveCheckpointList,
  getCheckpointByID
} = require("../../utils/initdata")

const {
  getPageConfig,
  saveRemoteRecord,
  saveRemoteGold
} = require("../../utils/custom_loading")

const {
  DefaultItemType,
  RightItemType,
  RedoItemType,
  getNextFlowItemByType,
} = require("../../utils/task")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    next_word: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 页面UI配置
    const config = getPageConfig('task')
    if (config) {
      this.setData({
        config: config
      })
    }

    // 页面显示数据
    this.checkpoint_list = getCheckpointList()
    this.checkpoint = getCheckpointByID(this.checkpoint_list, options.id)
    this.task = this.checkpoint.workflow.flow[this.checkpoint.workflow.step]
    const content = this.task.content.trim()
    this.setData({
      isWelcome: wx.getStorageSync('welcome') || this.checkpoint.status === 3,
      checkpoint: this.checkpoint,
      task: this.task,
      content: content == '' ? [] : content.split('\n'),
      next_word: this.checkpoint.status === 3 ? '点击退出' : '点击继续'
    })
  },

  onEndTask() {
    // 检查下一个流程是否是【关闭任务】
    const nextTask = this.checkpoint.workflow.flow[this.checkpoint.workflow.step + 1]
    // console.log('next task', nextTask)
    // TODO:处理晋级书
    if (nextTask.type === 300) {
      if (this.checkpoint.status < 3) {
        // 关闭任务，记录相关信息
        console.log('exit task')
        this.checkpoint.status = 3

        // 计算剩余任务
        let finishCount = 0
        // let totalCount = wx.getStorageSync('totalCount')
        let totalCount = 0
        this.checkpoint_list.forEach(function (checkpoint) {
          // 计算完成的任务数
          if (checkpoint.group === 'MAIN') {
            totalCount++
            if (checkpoint.status > 2) {
              // console.log(checkpoint)
              finishCount++
            }
          }
        })

        // 保存数据到本地
        saveCheckpointList(this.checkpoint_list)
        wx.setStorageSync('welcome', false)
        wx.setStorageSync('totalCount', totalCount)
        wx.setStorageSync('finishCount', finishCount)
        wx.setStorageSync('leftCount', totalCount - finishCount)

        // 保存数据到远程
        saveRemoteRecord()
      }

      // 任务已关闭，直接返回index
      wx.redirectTo({
        url: '../main/index',
      })

      return
    }

    // 保存流程数据
    this.checkpoint.workflow.step++
    saveCheckpointList(this.checkpoint_list)

    // 导航到task
    wx.redirectTo({
      url: '../main/task?id=' + this.checkpoint.code,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})